<?php
class Admin_MailController extends My_Controller_Admin_Action
{
	protected $mailModel = '';

	public function _init()
	{
		$this->mailModel = new Admin_Model_Mail();
	}

	public function indexAction()
	{
		$rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$rows = $this->mailModel->getListMail();
		$paginator = Zend_Paginator::factory($rows);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);

		$this->view->page = $page;
		$this->view->paginator = $paginator;
	}

	public function ajoutAction()
	{
		$id = $this->_getParam('id');
		if(!$id){
			$this->view->title = 'Ajout';
		}else{
			$this->view->title = 'Editer';
			$mail = new Admin_Model_Mail();
			$row = $mail->getMail($id);
			$this->view->mail = $row;
		}
	}

	public function saveMailAction()
	{
	    $post = $this->getRequest()->getPost();
	    $data = array(
	        'titre'    => $post['titre'],
	    	'contenu'  => $post['contenu'],
	    	'emailtest'=> $post['emailtest'],
	    	'dtcreat'  => date('y-m-d'),
	        'hrcreat'  => date('h:i:s'),
	        'dtstart'  => '0000-00-00',
	    	'dtdeleted'=> '0000-00-00',
	        'hrstart'  => '00:00:00',
	    	'num_temp' => md5(uniqid(rand(), true))
	    );

	    $mailModel = new Admin_Model_Mail();
	    $mailModel->insert($data);

    	$_POST[titre] = addslashes($_POST[titre]);
    	$_POST[contenu] = addslashes($_POST[contenu]);
    	$num_temp = md5(uniqid(rand(), true));

       	echo "<p>Le message a �t� sauvegard�.</p>";

    	if (isset($post[emailtest]) && $post[emailtest] != "") {
    		//include("_mailing_send.php");
    		//$this->sendMail();
    		echo "<p>Message de test envoy�.</p>";
    	}


    	$this->_redirect('admin/mail');

	}

    public function updateMailAction()
	{
	    $post = $this->getRequest()->getPost();
	    $mailModel = new Admin_Model_Mail();
	    $data = array(
	        'titre'    => $post['titre'],
	    	'contenu'  => $post['contenu'],
	    	'emailtest'=> $post['emailtest']
	    );

	    $mailModel->update($data, 'p_mailing = ' . $post['p_mailing']);

    	if (isset($post[emailtest]) && $post[emailtest] != "") {
    		//include("_mailing_send.php?numVente=$_GET[numVente]");
    		echo "<p>Message envoy�</p>";
    	}

        $this->_redirect('admin/mail');
	}

	public function deleteMailAction()
	{
		$id = $this->_getParam('id');
		$page = $this->_getParam('page', 1);
		$mailModel = new Admin_Model_Mail();
		$mailModel->removeMail($id);

		$param = '';
		if($page > 1) $param = 'index/page/' . $page;
		$this->_redirect('admin/mail/' . $param);
		exit;
	}

	public function sendAction()
	{
        $id = $this->_getParam('id', null);
        $domain = $this->getRequest()->getHttpHost();
        $mail = $this->mailModel->getMail($id);

        $view = new Zend_View();
        $view->setScriptPath(APPLICATION_PATH . '/modules/admin/views/scripts/mail/');
        $view->assign('content', $mail['contenu']);
        $view->assign('domain', $domain);
        $mailBody = $view->render('template.phtml');

        $config = array(
            'ssl' => 'ssl',
            'port' => 465,
            'auth' => 'login',
            'username' => 'thiendiachigiao@gmail.com',
            'password' => 'kaori***'
        );

        $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
        Zend_Mail::setDefaultTransport($transport);
	    $sender = new Zend_Mail();
	    $sender->setFrom('thiendiachigiao@gmail.com', 'Banque Dessinée');
	    $sender->addTo($mail['emailtest']);
	    $sender->setSubject($mail['titre']);
	    $sender->setBodyHtml($mailBody);
	    try {
    	    $sender->send();
    	    $data = array('tosend' => 1);
    	    $this->mailModel->save($data, $id);

    	    My_Message::addSuccess('Envoyer succès');
	    } catch (Exception $e) {
	        My_Message::addError('Envoyer failure');
	    }

        $page = $this->_getParam('page', 0);
        $params = $page ? '/index/page/' . $page : '';
        $this->_redirect('admin/mail' . $params);
	}
}

